package com.elektronikforumet.camalert;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.view.View;

/**
 * A simple View which is drawn to look like a (swedish) speed sign.
 * This widget will automatically scale to fit the available space.
 * 
 * @author Erik Abrahamsson
 */
public class SpeedsignView extends View {

	private int mSpeed, mW, mH;
	private boolean mDrawEnabled;
	
	public SpeedsignView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setMinimumWidth(120);
		setMinimumHeight(120);
		mSpeed = 0;
		mDrawEnabled = false;
	}

	/**
	 * Sets the number shown on the sign.
	 * @param num An integer representing the speed limit
	 */
	public void setSpeed(int speed) {
		mSpeed = speed;
		this.invalidate();
	}
	
	/**
	 * Sets the flag determining if the sign should med drawn.
	 * @param bool Drawing enabled
	 */
	public void setDrawEnabled(boolean bool) {
		mDrawEnabled = bool; 
		this.invalidate();
	}
	
    /**
     * @see android.view.View#measure(int, int)
     */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(
				measureWidth(widthMeasureSpec),
				measureHeight(heightMeasureSpec));
	}
	
	/**
	 * Determines the width of this view
	 * @param measureSpec A measureSpec packed into an int
	 * @return The width of the view, honoring constraints from measureSpec
	 */
	private int measureWidth(int measureSpec) {
		int result = getSuggestedMinimumWidth();
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		if (specMode == MeasureSpec.EXACTLY) {
			// We were told how big to be.
			result = specSize;
		}
		else if (specMode == MeasureSpec.AT_MOST) {
			result = Math.min(result, specSize);
		}

		return result;
	}

	/**
	 * Determines the height of this view
	 * @param measureSpec A measureSpec packed into an int
	 * @return The height of the view, honoring constraints from measureSpec
	 */
	private int measureHeight(int measureSpec) {
		int result = getSuggestedMinimumHeight();
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		if (specMode == MeasureSpec.EXACTLY) {
			// We were told how big to be, so nothing to do.
			result = specSize;
		}
		else if (specMode == MeasureSpec.AT_MOST) {
			result = Math.min(result, specSize);
		}

		return result;
	}

    /**
     * Render the text
     * 
     * @see android.view.View#onDraw(android.graphics.Canvas)
     */
	@Override
	protected void onDraw(Canvas canvas) {
		//Get current width and height of the View
		mW = getWidth();
		mH = getHeight();

		if (mDrawEnabled) {
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			//canvas.drawColor(Color.WHITE); //Used for debugging to see the canvas
	
			//Use the smallest of width and height
			int smallest = (mW < mH ? mW/2 : mH/2);
			
			//Paint a red circle
			paint.setColor(Color.RED);
			canvas.drawCircle(mW/2, mH/2, (float)(smallest * 0.95), paint);
	
			//Paint a yellow circle on top of the red
			paint.setColor(Color.YELLOW);
			canvas.drawCircle(mW/2, mH/2, (float)(smallest * 0.75), paint);
			
			//Draw the black text on top
			paint.setColor(Color.BLACK);
			paint.setTextSize(smallest);
			paint.setTextAlign(Align.CENTER);
			paint.setTextScaleX(0.85f);
			canvas.drawText(Integer.toString(mSpeed), mW/2, mH/2 + smallest/3, paint);
		}
}
}
